<template>
  <div>
    <h1>{{id?'编辑':'新建'}}英雄banner</h1>
    <el-form @submit.native.prevent="save" ref="form" :model="model" label-width="80px">
      <el-form-item label="英雄名称">
        <el-input v-model="model.name"></el-input>
      </el-form-item>
      <el-form-item>
        <el-upload
          class="avatar-uploader"
          :action="$http.defaults.baseURL + 'upload'"
          :show-file-list="false"
          :headers="getHeader()"
          :on-success="res=>$set(model,'banner',res.url)"
        >
          <img v-if="model.banner" :src="model.banner" class="banner" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" native-type="submit">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>


<script>
export default {
  props: {
    id: {}
  },
  data() {
    return {
      model: {}
    };
  },
  created() {
    this.id && this.feach();
  },
  methods: {
    async feach() {
      const res = await this.$http.get(`rest/newheros/${this.id}`);
      this.model = Object.assign({}, this.model, res.data); //res.data
    },
    async save() {
      let res;
      if (this.id) {
        //编辑
        res = await this.$http.put(`rest/newheros/${this.id}`, this.model);
      } else {
        //新建
        res = await this.$http.post("rest/newheros", this.model);
      }
      this.$router.push("/newheros/list");
      console.log(res.data);
      this.$message({
        message: "保存成功",
        type: "success"
      });
    }
  }
};
</script>
<style lang="css">
/*   .avatar{
    width: 10rem !important;
  } */
</style>